import { isEmpty } from 'shiptools'
import dayjs from 'dayjs'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    calData: {
      type: Array,
      value: () => [],
      observer: function (newVal, oldVal) { 
        
        if(newVal !== oldVal && !isEmpty(newVal)) {
          this._scrollToLeft(newVal)
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    scrollIntoView: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChooseCal: function(e) {
      const cal = e.currentTarget.dataset.cal
      if(!cal) return
      this.triggerEvent('choose', cal)
    },

    _scrollToLeft: function(calData) {

      const startDate = calData.filter(item => item.isSelected)[0]
      console.log("startDate: ", startDate)
      if(startDate) {
        const beforeDate = dayjs(startDate.dayFull).subtract(2, 'day').format('MM-DD')
        console.log(`into-${beforeDate}`)
        this.setData({
          scrollIntoView: `into-${beforeDate}`
        })
      }

    },

    onGoCalendar: function() {
      this.triggerEvent('calendar')
    }
  },
  lifetimes: {
    
    attached: function() {
      const that = this
    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行
    },
  
    error: function() {
      this.setData({
        errorState: true
      })
    }
  }

})
